#!/bin/sh
#

: << =cut

=head1 NAME

wmr_water - Plugin to monitor WMR weather water temperature sensors THWR288
wmr_water

=head1 CONFIGURATION

The following enviroment variables are used by this plugin:

 wmrconf - full path to config file to use. (Default /etc/wmr/wmr.conf)

=head1 USAGE

Place the plugin in the /etc/munin/plugins/ directory, and make it
executable.

=head1 AUTHOR

Oregon Scientific WMR100/200/WMRS200/I300/I600/RMS600 protocol.
Latest download URL: http://www.nkl.ru/support/wmr/
Global download URL: http://code.google.com/p/wmr/

Copyright (C) 2012 @Den68 <idg68@yandex.ru>

=cut

wmr_water=${0##*/}
sensor=${wmr_water##*_}
wmrconf=${wmrconf:-/etc/wmr/wmr.conf}
autoconfst=0

if [ "$1" = "autoconf" ]; then
        if [ ! -x "`which sqlite3`" ]; then
                echo "no '(sqlite3 not found)'"
                autoconfst=1
	fi

	if [ ! -f $wmrconf ]; then
                echo "no '(" $wmrconf " not found)'"
                autoconfst=1
	fi

	if [ $autoconfst == 0 ]; then
                echo yes
        fi

exit 0
fi

if [ "$1" = "config" ]; then
  echo "graph_category WeatherStation"
  echo "graph_title Temperature water (THWR288)"
  echo "graph_args --base 1000 --vertical-label degC"
  echo "temp_w.label Temperature water"
  echo "temp_w.draw LINE2"
exit 0
fi

SQLBASEPATH=`cat $wmrconf | grep SQLBASEPATH | awk '{ print $2 }'`
echo "temp_w.value" `sqlite3 -column ${SQLBASEPATH} "select temp from WATER where sensor = 0 ORDER BY tdate DESC LIMIT 1"`

exit 0
